package sk.tuke.constraintbuilder.model;

import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(name = "constraint", namespace="sk.tuke.constraintbuilder")
public class Constraint extends RootElement{

	private String annotationName;
	private Context context;
	private Severity severity;
	private ConditionsElement conditions = new ConditionsElement();
	private RulesElement rules = new RulesElement();
	
	public Constraint(){
		super();
		super.setName("New Constraint [id  "+super.getId()+"]");
	}
	
	public Constraint(Template template){
		this();
		this.setRules(template.getRule());
		this.setConditions(template.getCondition());
	}

	@XmlAttribute(name="annotation")
	public String getAnnotationName() {
		return annotationName;
	}

	public void setAnnotationName(String annotation) {
		this.annotationName = annotation;
	}

	@XmlAttribute(name="context")
	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	@XmlElement
	public ConditionsElement getConditions() {
		return conditions;
	}

	public void setConditions(ConditionsElement condition) {
		this.conditions = condition;
	}

	@XmlElement
	public RulesElement getRules() {
		return rules;
	}

	public void setRules(RulesElement rule) {
		this.rules = rule;
	}

	public Severity getSeverity() {
		return severity;
	}

	@XmlAttribute(name="severity")
	public void setSeverity(Severity severity) {
		this.severity = severity;
	}

	public boolean removeElement(RuleElement element) {
		boolean removed = false;
		if(rules!=null){
			 removed = rules.removeElement(element);
		}
		if(!removed && conditions!=null){
			removed = conditions.removeElement(element);
		}
		return removed;
	}
	
	public void deleteAllValues(){
		List<RuleElement> elements = rules.getElements();
		elements.addAll(conditions.getElements());
		for (RuleElement element : elements) {
			if(element instanceof SimpleRuleElement){
				((SimpleRuleElement) element).setValue(null);
			}
			else if(element instanceof StructuredRuleElement){
				IElementOfStructuredElement subElement = ((StructuredRuleElement) element).getElement();
				if(subElement instanceof Value){
					((Value) subElement).setValue(null);
				}
				else if(subElement instanceof INodeElement){
					((INodeElement) subElement).getElements().clear();
				}
			}
		}
	}
	
	

}
